package util;

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.naming.InitialContext;

public class MailImplement {
    
    private Properties props;
    private String mailhost;
    private String user;
    private String password;
    private String protocol;
    private String authorization;
    private String mailDisabled;
    private String verboseProp;
    private String debugProp;

    public MailImplement(){
        InitialContext ic;
        try {
            ic = new InitialContext();
            Session session = (Session) ic.lookup("mail/SSCEmailSession");
            props = session.getProperties();
            
            mailhost = props.getProperty("mail.smtp.host");
            user = props.getProperty("mail.smtp.user");
            password = props.getProperty("mail.smtp.password");
            protocol = props.getProperty("mail.transport.protocol");
         
            //authorization = props.getProperty("mail.smtp.auth");
            //mailDisabled = props.getProperty("mail.disable");
            //verboseProp = props.getProperty("mail.verbose");
            //debugProp = props.getProperty("mail.debug");
            
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        
    }
    
    public void sendSSLMessage(String recipient, String subject, String message, String from) throws MessagingException {
        Session session =
            Session.getDefaultInstance(props, new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(user, password);
                }
            });

        Message msg = new MimeMessage(session);
        InternetAddress addressFrom = new InternetAddress(from);
        msg.setFrom(addressFrom);

        InternetAddress addressTo = new InternetAddress(recipient);
        
        msg.setRecipient(Message.RecipientType.TO, addressTo);

        // Setting the Subject and Content Type
        msg.setSubject(subject);
        msg.setContent(message, "text/plain");
        Transport.send(msg);
        System.out.println("Sucessfully Sent mail to user");
    }
    
    
}
